//
//  IMChatVCUtil.swift
//  swiftim
//
//  Created by yangjian on 2023/7/18.
//

import Foundation


func msgListRowNeedShowTime(oldTimestamp : Int64 , newTimestamp : Int64) -> Bool {
    if oldTimestamp < 1 || newTimestamp < 1 {
        return true
    }
    let interval = newTimestamp - oldTimestamp
    if abs(interval) > 60000 {
        return true
    }
    return false
}

func generateTimeMsgModel(timestamp : Int64) -> MsgCellBaseModel {
    return MsgCellBaseModel(timestamp: timestamp)
}

func generateTimeText(timestamp : Int64) -> String {
    return timeStampToCurrennTime(timestamp: timestamp)
}

func timeStampToCurrennTime(timestamp: Int64) -> String {
    //获取当前的时间戳
    let currentTime = Date().timeIntervalSince1970
    //时间戳为毫秒级要 ／ 1000， 秒就不用除1000，参数带没带000
    let timeSta:TimeInterval = TimeInterval(timestamp / 1000)
    //时间差
    let reduceTime : TimeInterval = currentTime - timeSta
    //时间差小于60秒
    if reduceTime < 60 {
        return "刚刚"
    }
    //时间差大于一分钟小于60分钟内
    let mins = Int(reduceTime / 60)
    if mins < 60 {
        return "\(mins)分钟前"
    }
    //时间差大于一小时小于24小时内
    let hours = Int(reduceTime / 3600)
    if hours < 24 {
        return "\(hours)小时前"
    }
    let days = Int(reduceTime / 3600 / 24)
//    if days < 30 {
//        let date = NSDate(timeIntervalSince1970: timeSta)
//        let dfmatter = DateFormatter()
//        dfmatter.dateFormat="HH:mm:ss"
//        return "\(days)天前 \(dfmatter.string(from: date as Date))"
//    }
    if days < 2 {
        let date = NSDate(timeIntervalSince1970: timeSta)
        let dfmatter = DateFormatter()
        dfmatter.dateFormat="HH:mm:ss"
        return "昨天 \(dfmatter.string(from: date as Date))"
    }
    if days < 3 {
        let date = NSDate(timeIntervalSince1970: timeSta)
        let dfmatter = DateFormatter()
        dfmatter.dateFormat="HH:mm:ss"
        return "前天 \(dfmatter.string(from: date as Date))"
    }
    //不满足以上条件直接返回日期
    let date = NSDate(timeIntervalSince1970: timeSta)
    let dfmatter = DateFormatter()
    //yyyy-MM-dd HH:mm:ss
    dfmatter.dateFormat="yyyy年MM月dd日 HH:mm:ss"
    return dfmatter.string(from: date as Date)
}
